package com.qianfeng.gl4study.youshare.acitivity.store;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.qianfeng.gl4study.youshare.R;
import com.qianfeng.gl4study.youshare.model.CollectItem;
import com.qianfeng.gl4study.youshare.model.Product;
import com.qianfeng.gl4study.youshare.utils.*;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * I'm glad to share my knowledge with you all.
 * User:Gaolei
 * Date:2015/2/8
 * Email:pdsfgl@live.com
 */
public class PlatformProductInfoActivity extends Activity implements View.OnClickListener {

	private TextView txtCount;
	private Product product;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_platform_product_infofmation);
		ImageView imageView = (ImageView) findViewById(R.id.iv_product_info_image);
		TextView txtPriceYoushare = (TextView) findViewById(R.id.txt_product_info_price_youshare);
		TextView txtGuige = (TextView) findViewById(R.id.txt_product_info_guige);
		TextView txtAddress = (TextView) findViewById(R.id.txt_product_info_address);
		TextView txtPostPrice = (TextView) findViewById(R.id.txt_product_info_post_price);
		txtCount = (TextView) findViewById(R.id.txt_product_info_count);

		Button buy = (Button) findViewById(R.id.info_product_buy);
		Button collect = (Button) findViewById(R.id.info_product_collect);
		buy.setOnClickListener(this);
		collect.setOnClickListener(this);

		Intent intent = getIntent();
		Serializable pro = intent.getSerializableExtra("product");
		if(pro instanceof Product){
			product = (Product) pro;
			//显示商品信息
			txtPriceYoushare.setText(product.getShopPrice());
			txtGuige.setText(product.getAppGname());
			txtAddress.setText("河南");
			txtPostPrice.setText("6元");
			txtCount.setText("6");
			downloadImage(imageView, product);

		}
	}

	public void countAdd(View view){
		int count = Integer.parseInt(txtCount.getText().toString());
		txtCount.setText(String.valueOf(count+1));
	}

	public void countSul(View view){

		int count = Integer.parseInt(txtCount.getText().toString());
		if(count>1){
			txtCount.setText(String.valueOf(count-1));
		}
	}

	/**
	 * 开启异步任务,下载商品图片
	 * @param imageView
	 * @param product
	 */
	private void downloadImage(ImageView imageView, Product product) {
		//获取缩略图，采用相对路径，进行网址的处理
		String thumb = product.getGoodsImg();

		if(thumb!=null&&thumb.length()>0) {
			if (!thumb.startsWith("http://") && !thumb.startsWith("https://")) {
				thumb = Constants.IMAGE_SERVICE + thumb;
			}

			//进行图片下载之前设置ImageView的tag，异步任务检查tag
			imageView.setTag(thumb);

			//进行内存检查，如果有图片直接使用
			ImageCache imageCache = ImageCache.getInstance();
			Bitmap bitmap = imageCache.getImage(thumb);
			if(bitmap!=null){
				//包含图片对象，直接使用
				Log.d("YouShare", "Hit cache " + thumb);
				imageView.setImageBitmap(bitmap);
			}else {
				//不包含缓存，进行本地文件的加载
				FileCache fileCache = FileCache.getInstance();
				byte[] bytes = fileCache.getContent(thumb);
				if(bytes!=null&&bytes.length>0){

					Log.d("Duqu","运行到读取："+bytes.length);
					Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
					imageView.setImageBitmap(bmp);
					imageCache.putImage(thumb,bmp);
				}else {
					Log.d("YouShare","Load "+ thumb);
					ImageLoader imageLoader = new ImageLoader(imageView);
					imageLoader.execute(thumb);
				}
			}
		}
	}


	/**
	 * 单击购买或收藏时调用
	 * @param v
	 */
	@Override
	public void onClick(View v) {

		int id = v.getId();
		if(id==R.id.info_product_buy){
			//点击购买
			collectProduct();
			Toast.makeText(this,"添加购物车完成",Toast.LENGTH_LONG).show();
		}else if(id==R.id.info_product_collect){
			//点击收藏
			collectProduct();
			Toast.makeText(this,"收藏完成",Toast.LENGTH_LONG).show();
		}
		//收藏完成后，跳转到商品列表
		Intent intent = new Intent(this, StoreActivity.class);
		startActivity(intent);
		finish();
	}

	/**
	 * 收藏商品
	 * @return
	 */
	private CollectItem collectProduct(){

		CollectDatabasesManager databasesManager = CollectDatabasesManager.createInstance(this);
		CollectItem collectItem = new CollectItem();
		collectItem.setProductName(product.getName());
		//设置默认价格200元
		collectItem.setProductPrice(200.0f);
		collectItem.setProductThumb(product.getGoodsThumb());
		collectItem.setCount(Integer.parseInt(txtCount.getText().toString().trim()));
		collectItem.setProductId(product.getGoodsId());
		collectItem.setChecked(false);
		databasesManager.collectProduct(collectItem,Constants.USER_ID);

		return collectItem;
	}

}